﻿using UnityEngine;
using System.Collections;
using FloodFill;
using System.Collections.Generic;
using Assets.components;
using Assets.entities;
using Game.Scripts.Managers;

[RequireComponent(typeof(FrenzyIslandGenerator))]
[RequireComponent(typeof(VoxelPrototypeFactory))]
public class FrenzyIslandContentGenerator : MonoBehaviour {

    public BuildingManager buildingmanager;
    public List<GameObject> trees;
    public List<GameObject> palms;

    [Range(0, 100.0f)]
    public float noiseScale = 1.0f;

    [Range(0, 0.8f)] 
    public float PalmPercentage = 0.2f;

    [Range(0, 1.0f)]
    public float treeThreshold = 0.5f;

    private GameObject _worldContent;
    private FrenzyIslandGenerator frenzyGenerator;
    private VoxelPrototypeFactory prototypeFactory;
    
    void OnDestroy() {
        Destroy(_worldContent);
    }
	
	void Update () {
	
	}

    void Start()
    {
        
    }

    public void GenerateContent()
    {
        frenzyGenerator = GetComponent<FrenzyIslandGenerator>();
        prototypeFactory = GetComponent<VoxelPrototypeFactory>();

        //creo il parent
        _worldContent = CreateWorldContent();

        //algoritmo per generare alberi con il perling noise
        float xOrigin = Random.value * 100;
        float zOrigin = Random.value * 100;
        float rowCount = (float)frenzyGenerator.scannerField.rowCount;
        float colCount = (float)frenzyGenerator.scannerField.colCount;
        foreach (FrenzyIslandGenerator.VoxelInfo item in frenzyGenerator.ListGrassy)
        {
            Vector3 place = item.position;
            float xCoord = (xOrigin + place.x) / colCount * this.noiseScale;
            float zCoord = (zOrigin + place.z) / rowCount * this.noiseScale;
            float noise = Mathf.PerlinNoise(xCoord, zCoord);

            if (noise > this.treeThreshold){
                GameObject treePrefab = this.trees[Random.Range(0, this.trees.Count)];
                if (treePrefab != null) {
                    Vector3 position = new Vector3(place.x, place.y, place.z);
                    Voxel voxelAt = item.voxel;

                    Quaternion rotation = new Quaternion(0, Random.rotation.y, 0, 1);
                    GameObject tree = GameObject.Instantiate(treePrefab, position, rotation) as GameObject;
                    tree.transform.parent = this._worldContent.transform;
                    tree.transform.localScale = Vector3.one;
                    tree.isStatic = true;
                    item.voxel.content.informativeContent = tree;
                }
            }
        }

        //instanzio un pò di palme sulla spiaggia
        for (int i = 0; i < 30; i++) {
            GameObject palmPrefab = this.palms[Random.Range(0, this.palms.Count)];
            if (palmPrefab != null) {
                FrenzyIslandGenerator.VoxelInfo randomVoxel = frenzyGenerator.ListSandy[Random.Range(0, frenzyGenerator.ListSandy.Count)];
                Quaternion rotation = new Quaternion(0, Random.rotation.y, 0, 1);
                GameObject palm = Instantiate(palmPrefab, randomVoxel.position, rotation) as GameObject;
                palm.transform.localScale = Vector3.one;
                palm.transform.parent = _worldContent.transform;
                palm.isStatic = true;
                randomVoxel.voxel.content.informativeContent = palm;
            }
        }
    }

    /// <summary>
    /// Crea il parent che conterrà tutti gli oggetti
    /// </summary>
    private GameObject CreateWorldContent()
    {
        GameObject _worldContent = new GameObject("WorldContent");
        _worldContent.transform.position = Vector3.zero;
        //ancoro il world content all'isola
        _worldContent.transform.parent = transform.parent;  
        return _worldContent;
    }
}
